package com.blb.demo3;

import java.io.IOException;
import java.net.*;
import java.util.Random;

public class UDPClient {

    static Random random=new Random();

    public void sendMessage(String ip,int port,String message){

        try {
            DatagramSocket datagramSocket = new DatagramSocket();
            byte[] data = message.getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(data, 0, data.length, InetAddress.getByName(ip),port);
            datagramSocket.send(datagramPacket);

        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public static void main(String[] args) throws InterruptedException {
        String ip="192.168.1.100";

        while (true){
            int i = random.nextInt(10);
            String msg="眼乾洪好帅";
            for (int j = 0; j < i; j++) {
                msg+="*-";
            }
            Thread.sleep(10000);
            System.out.println(msg);
            new UDPClient().sendMessage(ip,8888,msg);
            new UDPClient().sendMessage("192.168.1.103",8888,msg);
            new UDPClient().sendMessage("192.168.1.100",8888,msg);
            new UDPClient().sendMessage(ip,8888,"我不再迷茫");
            new UDPClient().sendMessage(ip,8888,"思念是唯一的行囊");
            new UDPClient().sendMessage(ip,8888,"漫天的星光");
            new UDPClient().sendMessage(ip,8888,"总有一颗是你的愿望");
            new UDPClient().sendMessage(ip,8888,"前方道路不在孤单漫长");
            new UDPClient().sendMessage(ip,8888,"歌手：-------李翔------小李想睡觉");

            String message="*" +
                    "**" +
                    "***" +
                    "****" +
                    "*****" +
                    "******" +
                    "****" +
                    "***" +
                    "**" +
                    "*";
            new UDPClient().sendMessage(ip,8888,message);
        }
    }


}
